{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8fba6ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xlwings as xw"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d8cad61",
   "metadata": {},
   "source": [
    "### 知识点：用numpy.random生成全排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "de9623db",
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = np.random.permutation(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ebddbfc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 6, 5, 2, 3, 8, 0, 9, 7, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "069d02e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  7,  6,  3,  4,  9,  1, 10,  8,  5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认从0开始，转换成从1开始\n",
    "numbers + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffedd7c6",
   "metadata": {},
   "source": [
    "### 启动Excel的Xlwings App"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "741ea4f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = xw.App(visible=False, add_book=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4a1b2d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = app.books.open(\"教师资格考试汇总表.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "899ebe78",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheet = workbook.sheets[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14cc475d",
   "metadata": {},
   "source": [
    "### 计算学生个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f58321d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_number = sheet.range(\"A4\").expand('table').shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "817b28ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student_number"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98e172c3",
   "metadata": {},
   "source": [
    "### 生成随机全排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "42447e90",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_numbers = np.random.permutation(student_number) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f18cfac3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 16,  1,  6, 12,  5,  3,  2,  9, 17, 14,  8, 19, 10,  7, 18, 13,\n",
       "       20, 15,  4])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "47dad7dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheet.range(\"E4\").options(transpose=True).value = student_numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d85abb1c",
   "metadata": {},
   "source": [
    "### 保存工作表，关闭APP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b0d5e166",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook.save()\n",
    "app.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9084e0f6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
